//
//  VVSportPolyline.m
//  DingDingSport
//
//  Created by 王惠平 on 2017/4/22.
//  Copyright © 2017年 will wang. All rights reserved.
//

#import "VVSportPolyline.h"

@implementation VVSportPolyline

//oc2.0之前,@synthesize负责实现属性的set&get方法并且生成成员变量(不是"懒加载"机制), @property只负责声明属性的set&get方法,oc2.0开始,让@property既可以声明也可以实现属性的set&get方法

@synthesize time = _time;
@synthesize distance = _distance;

+ (instancetype)polylineWithSourceLocation:(CLLocation *)source andDestLocation:(CLLocation *)dest{
    
    //构造折线数据对象
    CLLocationCoordinate2D commonPolylineCoords[2];
    //起点  上一次的终点
    commonPolylineCoords[0].latitude = source.coordinate.latitude;
    commonPolylineCoords[0].longitude = source.coordinate.longitude;
    //终点
    commonPolylineCoords[1].latitude = dest.coordinate.latitude;
    commonPolylineCoords[1].longitude = dest.coordinate.longitude;
    
    //构造折线对象
    VVSportPolyline *commonPolyline = [VVSportPolyline polylineWithCoordinates:commonPolylineCoords count:2];
    //计算速度
    [commonPolyline calculateSpeedWithSoure:source andDest:dest];
    
    return commonPolyline;
}

//计算速度
- (void)calculateSpeedWithSoure:(CLLocation *)source andDest:(CLLocation *)dest{
    
    //(起点速度+终点速度) * 0.5  m/s -> km/hour   /1000 * 60 * 60
    _speed = (source.speed + dest.speed) * 0.5 * 3.6;
    
    _strokeColor = [UIColor colorWithRed:(_speed * 0.033) green:1 blue:0 alpha:1];

    //计算时间差
    _time = [dest.timestamp timeIntervalSinceDate:source.timestamp];

    //计算距离
    _distance = [dest distanceFromLocation:source] * 0.001;
}

- (CGFloat)time {
    
    return _time;
}

- (CGFloat)distance {
    
    return _distance;
}


@end
